package ihm.overlays;

import ihm.dialog.windows.EditProfileDialogWindow;
import ihm.main.Window;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Label;

import com.lo23.data.objinterface.LocalUser;

import tools.ImageConverter;
import tools.ImageUtils;
import tools.LocalUserIHMTest;
import tools.Test;

public class ProfileOverlay implements MainOverlay
{

	private Label lastNameTextArea;
	private Label firstNameTextArea;
	private Label birthDateTextArea;
	private Label addressTextArea;
	private Label eMailTextArea;
	private Label phoneNumberTextArea;
	private Label hobbiesTextArea;
	private Image picture;
	private Label profilePicture;
	private Label portTextArea;
	// TODO
	// private Label broadcastTextArea;
	private Button editProfileButton;

	private Composite composite;
	
	private boolean defaultProfilePicture = true;
	private static final String DEFAULT_IMAGE_PATH = "ressources/unicorn.jpg";

	private ProfileOverlay()
	{
	}

	public ProfileOverlay( Shell shell )
	{
		composite = new Composite( shell, SWT.NONE );
		composite.setLayout( new GridLayout( 1, true ) );
		composite.setLayoutData( new GridData( SWT.CENTER, SWT.UP, false, false ) );
		profilePicture = new Label( composite, SWT.None );
		lastNameTextArea = new Label( composite, SWT.NONE );
		firstNameTextArea = new Label( composite, SWT.NONE );
		birthDateTextArea = new Label( composite, SWT.NONE );
		addressTextArea = new Label( composite, SWT.NONE );
		eMailTextArea = new Label( composite, SWT.NONE );
		phoneNumberTextArea = new Label( composite, SWT.NONE );
		hobbiesTextArea = new Label( composite, SWT.NONE );
		portTextArea = new Label( composite, SWT.NONE );
		editProfileButton = new Button( composite, SWT.PUSH );
		editProfileButton.setText( "Modifier Profil" );
		editProfileButton.addSelectionListener( new SelectionAdapter() {
			public void widgetSelected( SelectionEvent e ) {
				new EditProfileDialogWindow();
			}
		});
	}

	public void updateProfile( LocalUser localUser )
	{
		try {
			if( localUser != null )
			{
				if( localUser.getProfilePicture() != null )
				{
					java.awt.Image image = Window.getInstance().getLocalUser().getProfilePicture().getImage();
					if( image != null )
					{
						picture = ImageConverter.makeSWTImage( Display.getCurrent(), image );
						// profilePicture.setImage( picture );
						profilePicture.setImage( new Image( Display.getCurrent(), ImageUtils.scaleImage( picture, 100, 100 )));
					}
				}
				else
				{
					if( defaultProfilePicture == true && picture == null )
					{
						picture = new Image( Display.getCurrent(), DEFAULT_IMAGE_PATH );
						// profilePicture.setImage( picture );
						profilePicture.setImage( new Image( Display.getCurrent(), ImageUtils.scaleImage( picture, 100, 100 )));
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		if( localUser != null )
		{
			lastNameTextArea.setText( localUser.getUserProfile().lastName );
			firstNameTextArea.setText( localUser.getUserProfile().firstName );
			// TODO - birthDateTextArea.setText( localUser.getUserProfile().birthDate );
			addressTextArea.setText( localUser.getUserProfile().address );
			eMailTextArea.setText( localUser.getUserProfile().email );
			phoneNumberTextArea.setText( localUser.getUserProfile().telNumber );
			hobbiesTextArea.setText( localUser.getUserProfile().hobbies );
			portTextArea.setText( "port " + localUser.getUserProfile().port );
		}
		composite.layout();

	}

}
